package com.axend.aerosense.pay.ui.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.axend.aerosense.pay.entity.PayRecordBean;
import com.axend.aerosense.pay.R;

import java.util.List;

public class PayPurchaseRecordAdapter extends RecyclerView.Adapter<PayPurchaseRecordAdapter.PayRecordViewHolder> {

    private List<PayRecordBean> recordLists;

    public PayPurchaseRecordAdapter(List<PayRecordBean> recordLists) {
        this.recordLists = recordLists;
    }

    @NonNull
    @Override
    public PayRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pay_purchase_record_item, parent, false);
        return new PayRecordViewHolder(view);
    }

    public void setRecordLists(List<PayRecordBean> recordLists) {
        this.recordLists = recordLists;
        notifyDataSetChanged();
    }

    @SuppressLint({"ResourceAsColor", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull PayRecordViewHolder holder, int position) {
        if (recordLists.size() != 0) {
            try{
                PayRecordBean payRecordBean = recordLists.get(position);
                //时间转换
                String gmtCreate = payRecordBean.getGmtPayment();
                String[] split = gmtCreate.split("\\.");
                String date = split[0].replace('T', ' ');

//            holder.buyMessageTv.setPadding(0, 0, 0, 0);
//            holder.buyMessageTv.setText(payRecordBean.getSubject());
//                holder.buyTimeTv.setText(holder.itemView.getContext().getText(R.string.pay_time) + ": " + date);
                holder.buyTimeTv.setText(date);
                holder.buyPriceTv.setText("$" + splitPrice(payRecordBean.getBuyerPayAmount()));
//            holder.buyTimeTv.setText(holder.itemView.getContext().getText(R.string.pay_time) + payRecordBean.getGmtPayment());
                holder.buyPriceTv.setText(holder.itemView.getContext().getText(R.string.pay_char) + splitPrice(payRecordBean.getBuyerPayAmount()));
            }catch (Exception e){

            }

        } else {
            holder.buyMessageTv.setText(R.string.pay_vip_purchase_record_false);
            holder.buyMessageTv.setTextColor(R.color.base_text_color_black);
//            layout_constraintHorizontal_bias
//            android:paddingLeft="150dp"
//            holder.buyMessageTv.setPadding(150,0,0,0);
            holder.itemView.setBackgroundColor(Color.argb(0, 0, 0, 0));
        }

    }

    /**
     * 价格拆分  如：9.00-->9,  9.99-->9.99
     *
     * @param price 浮点型价格
     * @return 字符串价格
     */
    public String splitPrice(double price) {
        String strPrice = String.valueOf(price);
        if (strPrice.contains(".")) {
            String[] split = strPrice.split("\\.");
            char[] chars = split[1].toCharArray();
            for (char aChar : chars) {
                if (aChar != '0') {
                    return strPrice;
                }
            }
            return split[0];
        }
        return strPrice;
    }

    @Override
    public int getItemCount() {
        return recordLists.size();
    }

    static class PayRecordViewHolder extends RecyclerView.ViewHolder {
        TextView buyMessageTv, buyTimeTv, buyPriceTv;

        public PayRecordViewHolder(@NonNull View itemView) {
            super(itemView);
            buyMessageTv = itemView.findViewById(R.id.pay_buy_message_tv);
            buyTimeTv = itemView.findViewById(R.id.pay_buy_time_tv);
            buyPriceTv = itemView.findViewById(R.id.pay_buy_price_tv);
        }
    }
}
